// pages/rooms/rooms.js
Page({
  data: {
    stories: [
      {
        id: 1,
        cover: 'https://image.shengxuankj.com/fronts/story1.png',
        title: '山间晨雾中的邂逅',
        summary: '清晨的第一缕阳光透过薄雾，洒在古朴的民宿庭院里，那一刻仿佛时间都静止了...'
      },
      {
        id: 2,
        cover: 'https://image.shengxuankj.com/fronts/story2.png',
        title: '老板娘的手工茶点',
        summary: '每天下午三点，老板娘都会亲手制作当地特色茶点，那份温暖的人情味让每位客人都难以忘怀'
      },
      {
        id: 3,
        cover: 'https://image.shengxuankj.com/fronts/story3.jpg',
        title: '星空下的篝火夜话',
        summary: '远离城市的喧嚣，在满天繁星下围坐篝火，听当地人讲述古老的传说故事'
      },
      {
        id: 4,
        cover: 'https://image.shengxuankj.com/fronts/story4.jpg',
        title: '梯田日出的震撼',
        summary: '凌晨五点起床，只为看到那壮观的梯田日出，金色的光芒洒满整个山谷'
      },
      {
        id: 5,
        cover: 'https://image.shengxuankj.com/fronts/story5.jpg',
        title: '古村落的慢时光',
        summary: '漫步在石板路上，听着潺潺流水声，感受着这个百年古村落的宁静与美好'
      },
      {
        id: 6,
        cover: 'https://image.shengxuankj.com/fronts/story6.jpg',
        title: '手工艺人的匠心',
        summary: '在民宿附近的小作坊里，遇见了坚持传统手工艺的老师傅，那份专注令人敬佩'
      }
    ],
    leftStories: [],
    rightStories: [],
    navHeight: wx.getStorageSync('navHeight') ? wx.getStorageSync('navHeight') : 88,
    navBarHeight: 0,
    tabBarHeight: 0,
    contentHeight: 0,
  },

  onLoad: function (options) {
    // 页面加载
    this.initWaterfall();
    
    // 计算剩余可视高度
    this.calculateContentHeight();
  },

  // 初始化瀑布流
  initWaterfall: function() {
    const stories = this.data.stories;
    const leftStories = [];
    const rightStories = [];

    stories.forEach((story,index) => {
      // 简单的瀑布流算法：将故事分配到高度较小的一列
      if (index % 2 == 0) {
        leftStories.push(story);
      } else {
        rightStories.push(story);
      }
    });

    this.setData({
      leftStories: leftStories,
      rightStories: rightStories
    });
  },
  /**
   * 计算主要内容区域的高度
   */
  calculateContentHeight() {
    const systemInfo = wx.getWindowInfo();
    const windowHeight = systemInfo.windowHeight;
    const safeAreaBottom = systemInfo.safeArea ? systemInfo.screenHeight - systemInfo.safeArea.bottom : 0;
    const tabBarHeight = 50 + safeAreaBottom; // 自定义tabbar高度(100rpx转px约50px) + 安全区域
    
    // 首页导航栏不占用空间，所以内容高度就是窗口高度减去tabbar高度
    const contentHeight = windowHeight;
    
    this.setData({
      tabBarHeight: tabBarHeight,
      contentHeight: contentHeight
    });
  },
  // 跳转到故事详情
  goToStoryDetail: function(e) {
    const story = e.currentTarget.dataset.story;
    wx.showToast({
      title: '查看故事：' + story.title,
      icon: 'none',
      duration: 2000
    });
    // 这里可以跳转到故事详情页面
    // wx.navigateTo({
    //   url: '/packageA/storyDetail/storyDetail?id=' + story.id,
    // })
  },

  // 返回上一页
  goBack: function() {
    wx.navigateBack();
  }
});